package com.lbd99.data.lbd.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lbd99.data.lbd.service.TTransferLogService;
import com.lbd99.data.lbd.service.TUserService;
import com.lbd99.data.lbd.vo.SysAccount;
import com.lbd99.data.lbd.vo.TUser;

@Controller
@RequestMapping("transfer")
public class TTransferLogController {
	 @Autowired
	 private TTransferLogService tTransferLogService;
	 @Autowired
	 private TUserService tUserService;
	
	/**
     * 分页查询数据
     * @param page
     * @param rows
     * @return
     */
	 @RequestMapping(value = "/getTransferRecord")
	 @ResponseBody
	 public Map<String, Object> getTransferRecord(int page,int rows,String phone,String startTime,String endTime) {
		 Map<String, Object> result = new HashMap<String, Object>();
		 //拼装map条件
		 Map<String,Object> map=new HashMap<String,Object>();
		 map.put("phone",phone);
		 if(StringUtils.isNotBlank(startTime)){
			map.put("startTime",startTime+" 00:00:00");
		 }
		 if(StringUtils.isNotBlank(endTime)){
			 map.put("endTime",endTime+" 23:59:59");
		 }
		 PageHelper.startPage(page, rows);
		 
		 List<Map<String,Object>> list = tTransferLogService.selectListMap(map);
		 PageInfo<Map<String,Object>> pages = new PageInfo<Map<String,Object>>(list);
		 result.put("total", pages.getTotal());
		 result.put("rows", pages.getList());
		return result;
	 }
	
	/**
     * 转账关系记录
     * @param page
     * @param rows
     * @return
     */
	 @RequestMapping(value = "/getTransferRelationRecord")
	 @ResponseBody
	 public Map<String, Object> getTransferRelationRecord(int page,int rows,String phone_out,String phone_in,HttpSession httpSession){
		 Map<String, Object> result = new HashMap<String, Object>();
		 //拼装map条件
		 Map<String,Object> map=new HashMap<String,Object>();
		 map.put("phone_out", StringUtils.trim(phone_out));
		 map.put("phone_in", StringUtils.trim(phone_in));
		 /*===================================================================================================*/
	     SysAccount sysAccount = (SysAccount)httpSession.getAttribute("SESSION_INFO_OBJECT");//获取当前登录的用户
	        if(sysAccount.getType()==1){//如果是普通管理员，用手机号查询出user信息
	        	TUser user = tUserService.selectByPhone(sysAccount.getTel());
	        	if(user!=null){
	        		//根据用户id查出该用户伞下的ID集合(包含自身)
	        		List<Integer> ids = tUserService.selectTreeIds(user.getId());
	        		if(ids!=null && ids.size()>0){
	        			map.put("ids",ids);
	        		}
	        	}
	        }
		 /*======================================================================================================*/
		 PageHelper.startPage(page, rows);
		 List<Map<String,Object>> list = tTransferLogService.selectRelationListByMap(map);
		 PageInfo<Map<String,Object>> pages = new PageInfo<Map<String,Object>>(list);
		 result.put("total", pages.getTotal());
		 result.put("rows", pages.getList());
		 return result;
	 }
	 
	 /**
     * 当天转账关系记录明细
     * @param page
     * @param rows
     * @return
     */
	 @RequestMapping(value = "/getTransferRelationRecordToday")
	 @ResponseBody
	 public Map<String, Object> getTransferRelationRecordToday(int page,int rows){
		 Map<String, Object> result = new HashMap<String, Object>();
		 PageHelper.startPage(page, rows);
		 List<Map<String,Object>> list = tTransferLogService.selectRelationListToday();
		 PageInfo<Map<String,Object>> pages = new PageInfo<Map<String,Object>>(list);
		 result.put("total", pages.getTotal());
		 result.put("rows", pages.getList());
		 return result;
	 }
	 
	 /**
     * 本周转账关系记录明细
     * @param page
     * @param rows
     * @return
     */
	 @RequestMapping(value = "/getTransferRelationRecordWeek")
	 @ResponseBody
	 public Map<String, Object> getTransferRelationRecordWeek(int page,int rows,HttpSession httpSession){
		 //拼装map条件
		 Map<String,Object> map=new HashMap<String,Object>();
		 /*===================================================================================================*/
	     SysAccount sysAccount = (SysAccount)httpSession.getAttribute("SESSION_INFO_OBJECT");//获取当前登录的用户
	        if(sysAccount.getType()==1){//如果是普通管理员，用手机号查询出user信息
	        	TUser user = tUserService.selectByPhone(sysAccount.getTel());
	        	if(user!=null){
	        		//根据用户id查出该用户伞下的ID集合(包含自身)
	        		List<Integer> ids = tUserService.selectTreeIds(user.getId());
	        		if(ids!=null && ids.size()>0){
	        			map.put("ids",ids);
	        		}
	        	}
	        }
		 /*======================================================================================================*/
		 Map<String, Object> result = new HashMap<String, Object>();
		 PageHelper.startPage(page, rows);
		 List<Map<String,Object>> list = tTransferLogService.selectRelationListWeek(map);
		 PageInfo<Map<String,Object>> pages = new PageInfo<Map<String,Object>>(list);
		 result.put("total", pages.getTotal());
		 result.put("rows", pages.getList());
		 return result;
	 }
	 
    @RequestMapping("test")
    @ResponseBody
    public Boolean test(Integer userid) {
        return tTransferLogService.compareCurrtimeAndTransfirsttime(userid);
    }
	 
}
